package action.operate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import bean.Operation;

/*
 * 对联系人进行管理操作
 */
public class ManageOperate {
	public String execute() {
		// 得到HttpSession方法
		HttpSession session = ServletActionContext.getRequest().getSession();
		// 得到HttpServletRequest方法
		HttpServletRequest request = ServletActionContext.getRequest();
		// 判断用户是否登录
		if ("true".equals(session.getAttribute("login"))) {
			Operation operation = new Operation();
			try {
				//如果switch内为空值，则会抛出空指针
				switch (request.getParameter("operate")) {

				case "del":
					operation.del(String.valueOf(session.getAttribute("id")),String.valueOf(request.getParameter("telid")));
					return "manage";

				case "add":
					operation.add(request.getParameter("name"), request.getParameter("tel"),
							request.getParameter("remarks"),request.getParameter("id"), String.valueOf(session.getAttribute("id")));
					return "manage";

				case "revise":
					operation.revise(request.getParameter("name"), request.getParameter("tel"),
							request.getParameter("remarks"), request.getParameter("oldName"),
							request.getParameter("oldTel"), request.getParameter("oldRemarks"),request.getParameter("telid"),
							String.valueOf(session.getAttribute("id")));
					return "manage";
				}
				return "manage";
			} catch (Exception e) {
				return "manage";
			}
		} else {
			return "login";
		}

	}
}
